<?php
declare(strict_types=1);
namespace cms\exam\api_controller;

use cms\exam\service\RoomService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use HyperfExt\Auth\Contracts\AuthManagerInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Class UserController
 * @package cms\exam\api_controller
 * @Controller(prefix="api/exam/room")
 */
class RoomController extends CommonController
{
    /**
     * @Inject()
     * @var RoomService
     */
    protected RoomService $roomService;

    /**
     * @return ResponseInterface
     * @RequestMapping(path="retrieve")
     */
    public function retrieve(): ResponseInterface
    {
        $app = $this->_getApp(true);
        $query = $this->roomService->model->where('app_id', $app->id);
        // region 查询条件部分 +++++
        $search = $this->request->input('search', '{}');
        $search = json_decode($search, true);
        if (isset($search['retest_enable'])) {
            $search['retest_enable'] = $search['retest_enable'] == 1 ? 1 : 2;
            $query->where('retest_enable', $search['retest_enable']);
        }
        var_dump($search);
        var_dump($search['retest_enable']);
        // endregion

        $page_size = (int) $this->request->input('page_size', 10);
        $page_size = $page_size > 0 ? min($page_size, 50) : 1;
        $result = $query->paginate($page_size);
        return $this->success('请求成功', [
            'page' => $result->currentPage(),
            'total' => $result->total(),
            'items' => $result->items()
        ]);
    }

    /**
     * @return ResponseInterface
     * @RequestMapping(path="detail")
     */
    public function detail(): ResponseInterface
    {
        $id = (int) $this->request->input('id', 0);
        $data = $this->roomService->getDataById($id, true);
        return $this->success('请求成功', $data);
    }
}